package com.talent.core.protocol.mqtt.handler;

import com.talent.core.protocol.mqtt.enums.TlMqttMessageType;
import com.talent.core.protocol.mqtt.model.fix.TlMqttFixedHead;
import com.talent.core.protocol.mqtt.model.request.TlMqttHeartBeatReq;
import com.talent.core.protocol.mqtt.model.response.TlMqttHeartBeat;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ClassName：ConnectHandler
 * @Author: hszhou
 * @Date: 2024/11/25 11:06
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
@ChannelHandler.Sharable
public class TlMqttHeartBeatHandler extends SimpleChannelInboundHandler<TlMqttHeartBeatReq> {

    private static final  Logger logger = LoggerFactory.getLogger(TlMqttHeartBeatHandler.class);

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, TlMqttHeartBeatReq msg) throws Exception {
        logger.debug("进入【PINGREQ】的处理器");

        TlMqttHeartBeat res = new TlMqttHeartBeat();
        int length=0;//消息id默认2个字节
        TlMqttFixedHead fixedHead = new TlMqttFixedHead();
        fixedHead.setMessageType(TlMqttMessageType.PINGRESP.getValue());
        fixedHead.setLength(length);
        res.setFixedHead(fixedHead);
        ctx.channel().writeAndFlush(res);
        logger.debug("【PINGRES】消息返回");
    }
}
